using System;
using System.Security.Permissions;
using Microsoft.Win32.SafeHandles;

namespace WindowsAPI
{
    public sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        // Methods
        [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
        internal SafeFindHandle() : base(true) { }

        public SafeFindHandle(IntPtr preExistingHandle, bool ownsHandle) : base(ownsHandle) { SetHandle(preExistingHandle); }

        protected override bool ReleaseHandle()
        {
            if(!(IsInvalid || IsClosed))
                return Kernel32.FindClose(this);
            return (IsInvalid || IsClosed);
        }

        protected override void Dispose(bool disposing)
        {
            if(!(IsInvalid || IsClosed))
                Kernel32.FindClose(this);
            base.Dispose(disposing);
        }

    }
}